package com.tianma315.admin.produce.controller;


import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

import com.tianma315.admin.common.base.BaseController;
import com.tianma315.admin.common.utils.Result;
import com.tianma315.admin.produce.VO.ScBatchingVO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.tianma315.admin.produce.domain.ScBatchingDO;
import com.tianma315.admin.produce.service.ScBatchingService;

/**
 * 
 * <pre>
 * 配料(生产)
 * </pre>
 * <small> 2019-05-30 09:53:17 | Aron</small>
 */
@Controller
@RequestMapping("/produce/scBatching")
public class ScBatchingController extends BaseController{
	@Autowired
	private ScBatchingService scBatchingService;
	
	@GetMapping()
//	@RequiresPermissions("produce:scBatching:scBatching")
	String ScBatching(){
	    return "produce/scBatching/scBatching";
	}

	@GetMapping("/{produceTaskId}")
	String ScBatching(@PathVariable("produceTaskId") Integer produceTaskId,Model model){
		List<ScBatchingDO> list = scBatchingService.getScBatchingDO(produceTaskId);
		Boolean flag = false;
		if (list==null || list.isEmpty()){
			flag = true;
		}
		model.addAttribute("produceTaskId",produceTaskId);
		model.addAttribute("flag",flag);
		return "produce/scBatching/scBatching";
	}
	
	@ResponseBody
	@GetMapping("/list")
//	@RequiresPermissions("produce:scBatching:scBatching")
	public Result<Page<ScBatchingVO>> list(Integer pageNumber, Integer pageSize, ScBatchingDO scBatchingDTO){
		// 查询列表数据
        Page<ScBatchingVO> page = scBatchingService.getScBatchingVO(pageNumber,pageSize,scBatchingDTO);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
//	@RequiresPermissions("produce:scBatching:add")
	String add(){
	    return "produce/scBatching/add";
	}

	@GetMapping("/add/{produceTaskId}")
	String add(@PathVariable("produceTaskId")Integer produceTaskId,Model model){
		model.addAttribute("produceTaskId",produceTaskId);
		return "produce/scBatching/add";
	}

	@GetMapping("/edit/{batchingId}")
//	@RequiresPermissions("produce:scBatching:edit")
	String edit(@PathVariable("batchingId") Integer batchingId,Model model){
		ScBatchingDO scBatching = scBatchingService.selectById(batchingId);
		model.addAttribute("scBatching", scBatching);
	    return "produce/scBatching/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
//	@RequiresPermissions("produce:scBatching:add")
	public Result<String> save( ScBatchingDO scBatching){
		scBatching.setCreateBy(getUserId().toString());
		scBatching.setCreateDate(Calendar.getInstance().getTime());
		if(scBatchingService.addScBatchingVO(scBatching)){
			return Result.ok();
		}
		return Result.fail();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
//	@RequiresPermissions("produce:scBatching:edit")
	public Result<String>  update( ScBatchingDO scBatching){
		scBatchingService.updateById(scBatching);
		return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
//	@RequiresPermissions("produce:scBatching:remove")
	public Result<String>  remove( Integer batchingId){
		if(scBatchingService.deleteScBatchingVO(batchingId)){
            return Result.ok();
		}
		return Result.fail();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
//	@RequiresPermissions("produce:scBatching:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Integer[] batchingIds){
		for (int i = 0; i < batchingIds.length; i++) {
			scBatchingService.deleteScBatchingVO(batchingIds[i]);
		}
		return Result.ok();
	}
	
}
